/*
 * 病人列表：候诊（同步异步）、我的病人（同步异步）、本科病人（异步,点击页签加载）、完诊列表（异步,点击页签加载）
 * */
//候诊异步
function asyncHouzhenTable(days){
	houzhenPatientList(days,true);
}
//候诊同步
function TasyncHouzhenTable(){
	houzhenPatientList(0,false);
}
//我的病人异步
function asyncMyPatientTable(){
	myJienzhenPatientList(true);
}
//我的病人同步
function TasyncMyPatientTable(){
	myJienzhenPatientList(false);
}
//候诊病人列表
function houzhenPatientList(days,isasync) {
	var whichDay = "today";
	if(days == 1){//昨天
		beginTime = getDay(-2)+" 23:45:00:";
		endTime = getDay(-1)+" 23:59:59:";
		whichDay = "yesterday";
		
	}else if(days == 2){ //前天
		beginTime = getDay(-3)+" 23:45:00:";
		endTime = getDay(-2)+" 23:59:59:";
		whichDay = "thedaybeforeyesterday";
	}else{  //今天
		beginTime = getDay(-1)+" 23:45:00:";
		endTime = getDay(0)+" 23:59:59:";
		whichDay = "today";
	}
	if(isasync == undefined){
		isasync = true;
	}
	var jiezhenfw = valueArr.p_100_105002_100;
	$.ajax({
		url : rootUrl+ "/patientsearch/querywaitingandtransfertreatment_one",
		data : {
			lPara : jiezhenfw,
			lVAC51 : 0,
			lBCK01 : $("#departmentList").find("option:selected").attr("id"),
			lBCE01 : bce01,
			BAS02 : 0001,
			fenzhen : fenzhen,
			vad01 : 0,//是否启用过滤未分诊的病人（0：不启用，1：启用）
			beginTime :beginTime,
			endTime :endTime,
			whichDay:whichDay
		},
		type : "get",
		dataType : 'json',
		async:isasync,
		success : function(data) {
			var houzhenpatientList = data.list;
			for (var i = 0; i < houzhenpatientList.length; i++) 
			{
				houzhenpatientList[i].name = (houzhenpatientList[i]["vaa05"]).toPinYin();
			}
			houzhenTbleList = houzhenpatientList;
			$("#patient_wait").GridUnload();
			$("#patient_wait").jqGrid( {
		    	  data:houzhenpatientList,
		          datatype : "local",
		          colModel : [ 
		                      {name : "vad21",label:"号码",width:35}, 
		                      {name : "vaa03",label:"门诊号",width:75}, 
		                      {name : "vaa05",label:"姓名",width:60},
		                      {name : "abw02",label:"性别",width:35}, 
		                      {name : "agep",label:"年龄",width:45}, 
		                      {name : "bce03a",label:"就诊医师",width:70}, 
		                      {name : "vac43",label:"来源",width:40,
		                    	  formatter:function(cellvalue, options, rowObject){
		                              switch (cellvalue)
		                              {
		                                case 0 :
		                                  return "正常";
		                                  break;
		                                case 1 :
		                                	return "预约";
		                                	break;
		                                case 2 :
		                                  return "微信";
		                                  break;
		                                case 3 :
		                                	return "CRM";
		                                	break;
		                                default :
		                                  return "";
		                                  break;
		                              }
		                            },
		                            cellattr:addCellAttr1
		                     }, 
		                     
		                      {name : "vac10",label:"急诊",formatter:"checkbox",width:50}, 
		                      {name : "vac06",label:"复诊",formatter:"checkbox",width:50}, 
		                      {name : "bck03",label:"就诊科室",width:200}, 
		                      {name : "bdp02",label:"病人类别",width:100}, 
		                      {name : "vac36",label:"挂号时间",width:200,formatter:'date', formatoptions:{srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s'}}, 
		                      {name : "vac02",label:"就诊号",width:200},
		                      {name : "vac01",hidden:true}, 
		                      {name : "name",hidden:true}, 
		                      {name : "bas02a",hidden:true}, 
		                      {name : "vad17",hidden:true}, 
		                      {name : "vaa17",hidden:true}, 
		                      {name : "vaa02",hidden:true}, 
		                      {name : "iax04",hidden:true}, 
		                      {name : "vaa10",hidden:true}, 
		                      {name : "vcj04",hidden:true}, 
		                      {name : "vcj05",hidden:true}, 
		                      {name : "vaa01",hidden:true}, 
		                      {name : "bck01a",hidden:true}, 
		                    ],
		          height : 'auto',
		      	  width : 'auto',
		      	  rowNum : houzhenpatientList.length,
		      	  afterInsertRow: function(rowid, rowData, rowelem) {
		     		isNormalBloodPressure(rowid,rowData,"patient_wait");
					
				  },
		      	  onSelectRow : function(rowid,status)
		    	  {
		      		 wanzhenType = true;
		      		 changeButtonState();
		    		 row = $("#patient_wait").jqGrid("getRowData", rowid);
		    		 isOwnchose = false;
		    		 getPatientDept(row.bck01a);
		    		 reloadPatientInfo(row.vac01);
		    	  },
		    	  ondblClickRow:function(rowid,status)
		    	  {
		    		var ifdo = valueArr.p_100_105002_270;
		    		if(ifdo !=1){
		    			wanzhenType = true;
		    			changeButtonState();
		    			var row = $("#patient_wait").jqGrid("getRowData", rowid);
		    			$("#vac01").val(row.vac01);
		    			$("#vaa01").val(row.vaa01);
		    			isChoseWait = true;
		    			hujiao_houtai();
		    			
		    		 }    
		    	  },
		    	  loadComplete : function(data)
		    	  {
		    		$("#reflush_patient_wait").removeAttr("disabled");
		    		if(data.records == 0){
		    			var $tr = $('#patient_wait').find('tbody>tr:last');
						var len = $tr.find('td').length;
						$("#patient_wait").html('<tr><td colspan="'+ parseInt(len)+ '" style="text-align:left;">暂无数据！</td></tr>');
		    		}
		    	}
		    });
		}
	});
}
//接诊病人查询接口
function jieZhenList(info){
	var def = $.Deferred();
	if(info.isasync == undefined){
		isasync = true;
	}
	var vaa01 = $("#vaa01").val();
	var dl1 = "'"+$("#dl1").val()+ "'";
	var dl2 =  "'"+$("#dl2").val()+ "'";
	if(!dl2 ||!dl1){
		return false;
	}
	var vac01=$("#vac01").val();
	var text = "'%"+$("#houzheSearchStr").val()+"%'";
	var data = {
		sqlId:10000075,
		lVAC34 : info.lVAC34,
		lDt1 : dl1,
		lDt2 : dl2,
		lPara : info.jiezhenfw,
		lBCK01 : bck01, 
		lBCE01 : bce01,
		BAS02 : 0001,
		currPage : 1,
		pageSize : 500,
		txt :text
	};
    var param = {params:JSON.stringify(data)};
    $.ajax({
    	type: "GET",
    	url: rootUrl+"/basicinformationquery/basequery",
    	data:param,
    	dataType: "json",
    	async : info.isasync,
    	success: function (resp) {
    		debugger;
    		def.resolve(resp);
    	}
    });
    return def.promise();
}

//我的接诊病人列表
function myJienzhenPatientList(isasync) {
	debugger;
	var info = {};
	info.isasync =isasync;
	info.lVAC34 = 1;
	info.jiezhenfw =  '06';//全院本人号
	var vac01_url = getUrlParam("vac01");
	$.when(
		  jieZhenList(info)
        ).done(function (data) {
			var houzhenpatientList = data.list;
			var html = "";
			for (var i = 0; i < houzhenpatientList.length; i++) {
				houzhenpatientList[i].name = (houzhenpatientList[i]["vaa05"]).toPinYin();
			} 
			myJieZhenTbleList = houzhenpatientList;
			$("#myjiezhenTableList").GridUnload();
			$("#myjiezhenTableList").jqGrid({
		    	  data:houzhenpatientList,
		          datatype : "local",
		          colModel : [ 
		                      {name : "vac01",hidden:true}, 
		                      {name : "bck01a",hidden:true}, 
		                      {name : "name",hidden:true}, 
		                      {name : "vac72",hidden:true}, 
		                      {name : "vaa02",hidden:true}, 
		                      {name : "vaa17",hidden:true}, 
		                      {name : "iax04",hidden:true}, 
		                      {name : "vaa10",hidden:true}, 
		                      {name : "vcj04",hidden:true}, 
		                      {name : "vcj05",hidden:true}, 
		                      {name : "vaf36",hidden:true}, 
		                      {name : "vaa03",label:"门诊号",width:80}, 
		                      {name : "vaa05",label:"姓名",width:80}, 
		                      {name : "abw02",label:"性别",width:35},
		                      {name : "agep",label:"年龄",width:45},
		                      {name : "vac43",label:"来源",width:40,
		                    	  formatter:function(cellvalue, options, rowObject){
		                              switch (cellvalue)
		                              {
		                                case 0 :
		                                  return "正常";
		                                  break;
		                                case 1 :
		                                	return "预约";
		                                	break;
		                                case 2 :
		                                  return "微信";
		                                  break;
		                                case 3 :
		                                	return "CRM";
		                                	break;
		                                default :
		                                  return "";
		                                  break;
		                              }
		                            },
		                            cellattr:addCellAttr1
		                     }, 
		                     
		                      {name : "vac10",label:"急诊",formatter:"checkbox",width:50}, 
		                      {name : "vac06",label:"复诊",formatter:"checkbox",width:50}, 
		                      {name : "bck03",label:"就诊科室",width:200}, 
		                      {name : "bdp02",label:"病人类别",width:100}, 
		                      {name : "bce03a",label:"就诊医师",width:90}, 
		                      {name : "vac35",label:"接诊时间",width:200,formatter:'date', formatoptions:{srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s'}}, 
		                      {name : "vac02",label:"就诊号",width:200}
		                    ],
		          rownumbers:true,
		          height : 'auto',
		      	  width : 'auto',
		      	  rowNum : houzhenpatientList.length,
		          afterInsertRow: function(rowid, rowData, rowelem) {
		     		isNormalBloodPressure(rowid,rowData,"myjiezhenTableList");
		     		if(vac01_url && rowData.vac01 == vac01_url){
		    			$("#myjiezhenTableList").jqGrid("setSelection", rowid);
		    			$("#newpage").trigger("click");
		    		}
					
				  },
		      	  onSelectRow : function(rowid,status)
		    	  {
		      		 wanzhenType = true;
		      		 changeButtonState();
		    		 var row = $("#myjiezhenTableList").jqGrid("getRowData", rowid);
		    		 $("#date1_p").val(row.vaf36);
		    		 isOwnchose = true;
//		    		 getPatientDept(row.bck01a);
		    		 reloadPatientInfo(row.vac01);
		    	  },
		    	  ondblClickRow:function(rowid,status)
		    	  {
		    		wanzhenType = true;
		    		changeButtonState();
		    		 row = $("#myjiezhenTableList").jqGrid("getRowData", rowid);
		    		 vac01 = row.vac01
		    		 isOwnchose = true;
		    		 $("#myjiezhenTableList").setSelection(rowid);
		    		 if(vac01!="null")
		    		 {
		    			 $("#newpage").click();
					 }
		    	   },
		    	 loadComplete : function(data)
		    	 {
		    		$("#search_patinet").removeAttr("disabled");
		    		if(!vac01_url){
		    			$("#myjiezhenTableList").jqGrid("setSelection", 1);
		    		}
		    		if(data.records == 0){
		    			var $tr = $('#myjiezhenTableList').find('tbody>tr:last');
						var len = $tr.find('td').length;
						$("#myjiezhenTableList").html('<tr><td colspan="'+ parseInt(len)+ '" style="text-align:left;">暂无数据！</td></tr>');
		    		}
		    	 }
		    });
		 
    });
}
//本科室的接诊病人列表
function benkeJiezhenPatientList() {
	debugger;
	var info = {};
	info.isasync =true;
	info.lVAC34 = 1;
	info.jiezhenfw =  '03';
	var vac01_url = getUrlParam("vac01")
	$.when(
		  jieZhenList(info)
        ).done(function (data) {

			var houzhenpatientList = data.list;
			var html = "";
			for (var i = 0; i < houzhenpatientList.length; i++) {
				houzhenpatientList[i].name = (houzhenpatientList[i]["vaa05"]).toPinYin();
			}
			benkeZhenTbleList = houzhenpatientList;
			$("#jiezhenBenkeTable").GridUnload();
			$("#jiezhenBenkeTable").jqGrid({
		    	  data:houzhenpatientList,
		          datatype : "local",
		          colModel : [ 
		                      {name : "vac01",hidden:true}, 
		                      {name : "bck01a",hidden:true}, 
		                      {name : "name",hidden:true}, 
		                      {name : "vac72",hidden:true}, 
		                      {name : "vaa02",hidden:true}, 
		                      {name : "vaa17",hidden:true}, 
		                      {name : "iax04",hidden:true}, 
		                      {name : "vaa10",hidden:true}, 
		                      {name : "vcj04",hidden:true}, 
		                      {name : "vcj05",hidden:true}, 
		                      {name : "vaf36",hidden:true}, 
		                      {name : "vaa03",label:"门诊号",width:80}, 
		                      {name : "vaa05",label:"姓名",width:80}, 
		                      {name : "abw02",label:"性别",width:35},
		                      {name : "agep",label:"年龄",width:45},
		                      {name : "vac43",label:"来源",width:40,
		                    	  formatter:function(cellvalue, options, rowObject){
		                              switch (cellvalue)
		                              {
		                                case 0 :
		                                  return "正常";
		                                  break;
		                                case 1 :
		                                	return "预约";
		                                	break;
		                                case 2 :
		                                  return "微信";
		                                  break;
		                                case 3 :
		                                	return "CRM";
		                                	break;
		                                default :
		                                  return "";
		                                  break;
		                              }
		                            },
		                            cellattr:addCellAttr1
		                     }, 
		                       
		                      {name : "vac10",label:"急诊",formatter:"checkbox",width:50}, 
		                      {name : "vac06",label:"复诊",formatter:"checkbox",width:50}, 
		                      {name : "bck03",label:"就诊科室",width:200}, 
		                      {name : "bdp02",label:"病人类别",width:100}, 
		                      {name : "bce03a",label:"就诊医师",width:90}, 
		                      {name : "vac35",label:"接诊时间",width:200}, 
		                      {name : "vac02",label:"就诊号",width:200}
		                    ],
		          rownumbers:true,
		          height : 'auto',
		      	  width : 'auto',
		      	  rowNum : houzhenpatientList.length,
		      	  afterInsertRow: function(rowid, rowData, rowelem) {
		     		 isNormalBloodPressure(rowid,rowData,"jiezhenBenkeTable");
					
				  },
		      	  onSelectRow : function(rowid,status)
		    	  {
		      		debugger;
		      		row = $("#jiezhenBenkeTable").jqGrid("getRowData", rowid);
		      		 if (row.bce03a!=bce03)
					{
						wanzhenType = false;
					}
		      		 else
		      		 {
		      			wanzhenType = true;
		      		 }
					 changeButtonState();
		    		 isOwnchose = true;
		    		 $("#date1_p").val(row.vaf36);
		    		 reloadPatientInfo(row.vac01);
		    	  },
		    	  ondblClickRow:function(rowid,status)
		    	  {
		    		 var row = $("#jiezhenBenkeTable").jqGrid("getRowData", rowid);
		      		 if (row.bce03a!=bce03)
					 {
						wanzhenType = false;
					 }
		      		 else
		      		 {
		      			wanzhenType = true;
		      		 }
		    		 changeButtonState();
		    		 isOwnchose = true;
		    		 	$("#jiezhenBenkeTable").setSelection(rowid);
		    	     },
		    	  loadComplete : function(data)
		    	  {
		    		$("#search_patinet").removeAttr("disabled");
		    		if(data.records == 0){
		    			var $tr = $('#jiezhenBenkeTable').find('tbody>tr:last');
						var len = $tr.find('td').length;
						$("#jiezhenBenkeTable").html('<tr><td colspan="'+ parseInt(len)+ '" style="text-align:left;">暂无数据！</td></tr>');
		    		}
		    	  }
		    });
		 
        	
        })
//	var dl1 = $("#dl1").val();
//	var dl2 = $("#dl2").val();
//	var jiezhenfw = '03';
//	$.ajax({
//		url : rootUrl + "/patientsearch/queryvisitdoctor",
//		data : {
//			lVAC34 : 1,
//			lDt1 : dl1,
//			lDt2 : dl2,
//			lPara : jiezhenfw,
//			VAC01 : 0,
//			lBCK01 : $("#departmentList").find("option:selected").attr("id"),
//			lBCE01 : bce01,
//			BAS02 : 0001,
//			currPage : 1,
//			pageSize : 50
//		},
//		type : "get",
//		dataType : 'json',
//		success : function(data) {}
//	});
}
/**
 * 完诊病人 ， 时间获取函数，今天，昨天，前天
 */
var wanzhenType = true;
function loadWabZhenPatient(){
	debugger;
	var info = {};
	info.isasync =true;
	info.lVAC34 = 2;
	info.jiezhenfw =  '04';
	var vac01_url = getUrlParam("vac01")
	$.when(
		  jieZhenList(info)
        ).done(function (data) {

			var html = "";
			for (var i = 0; i < data.list.length; i++) {
				data.list[i].name = data.list[i]["vaa05"].toPinYin();
			} 
			wanZhenTbleList = data.list
			$("#wanzhenBenkeTable").GridUnload();
			$("#wanzhenBenkeTable").jqGrid({
		    	  data:wanZhenTbleList,
		          datatype : "local",
		          colModel : [ 
		                      {name : "vaa03",label:"门诊号",width:80}, 
		                      {name : "vaa05",label:"姓名",width:80}, 
		                      {name : "abw02",label:"性别",width:35},
		                      {name : "agep",label:"年龄",width:45},
		                      {name : "vac43",label:"来源",width:40,
		                    	  formatter:function(cellvalue, options, rowObject){
		                              switch (cellvalue)
		                              {
		                                case 0 :
		                                  return "正常";
		                                  break;
		                                case 1 :
		                                	return "预约";
		                                	break;
		                                case 2 :
		                                  return "微信";
		                                  break;
		                                case 3 :
		                                	return "CRM";
		                                	break;
		                                default :
		                                  return "";
		                                  break;
		                              }
		                            },
		                            cellattr:addCellAttr1
		                     }, 
		                     
		                      {name : "vac10",label:"急诊",formatter:"checkbox",width:50}, 
		                      {name : "vac06",label:"复诊",formatter:"checkbox",width:50}, 
		                      {name : "bck03",label:"就诊科室",width:200}, 
		                      {name : "bdp02",label:"病人类别",width:100}, 
		                      {name : "bce03a",label:"就诊医师",width:90}, 
		                      {name : "vac35",label:"接诊时间",width:200,formatter:'date', formatoptions:{srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s'}}, 
		                      {name : "vac02",label:"就诊号",width:200},
		                      {name : "vac01",hidden:true}, 
		                      {name : "name",hidden:true}, 
		                      {name : "vac72",hidden:true}, 
		                      {name : "vaa02",hidden:true},
		                      {name : "vaf36",hidden:true}
		                    ],
		          rownumbers:true,
		          height : 'auto',
		      	  width : 'auto',
		      	  rowNum : wanZhenTbleList.length,
		      	  onSelectRow : function(rowid,status)
		    	  {
		      		wanzhenType = false;
		      		changeButtonState();
		    		var row = $("#wanzhenBenkeTable").jqGrid("getRowData", rowid);
		    		 $("#date1_p").val(row.vaf36);
		    		reloadPatientInfo(row.vac01);
		    	  },
		    	  ondblClickRow:function(rowid,status)
		    	  {
		    		wanzhenType = false;
		      	 	changeButtonState();
		    	  },
		    	  loadComplete : function(data)
		    	  {
		    		$("#search_patinet").removeAttr("disabled");
		    		if(data.records == 0){
		    			var $tr = $('#wanzhenBenkeTable').find('tbody>tr:last');
						var len = $tr.find('td').length;
						$("#wanzhenBenkeTable").html('<tr><td colspan="'+ parseInt(len)+ '" style="text-align:left;">没有符合条件的数据！</td></tr>');
		    		 }
		    	  }
		    });
        })
//	    var jiezhenfw = '04';//本人号
//		var dl1 = $("#dl1").val();
//		var dl2 = $("#dl2").val();
//		$.ajax({
//		url : rootUrl + "/patientsearch/queryvisitdoctor",
//		data : {
//			lVAC34 : 2,
//			lDt1 : dl1,
//			lDt2 : dl2,
//			lPara : jiezhenfw,
//			VAC01 : 0,
//			lBCK01 : $("#departmentList").find("option:selected").attr("id"),
//			lBCE01 : bce01,
//			BAS02 : 0001,
//			currPage : 1,
//			pageSize : 500
//		},
//		type : "get",
//		dataType : 'json',
//		success : function(data) {}
//	});
}
function getDay(day){  
    var today = new Date();  
      
    var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;          

    today.setTime(targetday_milliseconds); //注意，这行是关键代码
      
    var tYear = today.getFullYear();  
    var tMonth = today.getMonth();  
    var tDate = today.getDate();  
    tMonth = doHandleMonth(tMonth + 1);  
    tDate = doHandleMonth(tDate);  
    return tYear+"-"+tMonth+"-"+tDate;  
}  
function doHandleMonth(month){  
    var m = month;  
    if(month.toString().length == 1){  
       m = "0" + month;  
    }  
    return m;  
} 
function doHandleMonth(month){  
    var m = month;  
    if(month.toString().length == 1){  
       m = "0" + month;  
    }  
    return m;  
}

/*document.write("前天："+getDay(-2));  
document.write("<br />昨天："+getDay(-1));  
document.write("<br />今天："+getDay(0));  
document.write("<br />明天："+getDay(1));  
document.write("<br />后天："+getDay(2));  
document.write("<br />大后天："+getDay(3));  */

//取消完诊
function quxiaowanzhen(){
	var rowid = $("#wanzhenBenkeTable").jqGrid('getGridParam','selrow');
	var row = $("#wanzhenBenkeTable").jqGrid("getRowData", rowid);
	debugger;
	if (rowid) {
		$.post(rootUrl+"/doctoradvice/quxiaowanzhen",{vac01:$("#vac01").val()},function(data){
			if(data && data.result=="Success")
			{
				//刷新接诊病人和完诊病人
				myJienzhenPatientList();
				loadWabZhenPatient();
				layer.msg("取消完诊成功！", {
					time : 2000,
					icon : 1,
					skin: 'layui-layer-msg1'
				});
			}
			else
			{
				layer.msg("取消完诊失败！", {
					time : 2000,
					icon : 0,
					skin: 'layui-layer-msg0'
				});
			}
		})
	} else {
		layer.msg("请选择要取消完诊的病人", {
			time : 2000,
			icon : 0,
			skin: 'layui-layer-msg0'
		});
	}
}







